import React, {PureComponent} from "react";
import {
    Platform,
    StyleSheet,
    ART,
    Text,
    View,
    ScrollView,
    Image,
    Dimensions,
    TouchableOpacity,
    Button,
    DeviceEventEmitter,
    NativeModules,
    NativeEventEmitter,
} from "react-native";
import MineSettingScene from "./settings/MineSettingScene";
import MineItemCell from "../../widget/mine/MineItemCell";
import MineAddressScene from "./address/MineAddressScene";
import api from "../../api/api";
import method from "../../api/method";
import {fetchGetRequest} from "../../api/NetworkTool";
import DeviceStorage from "../../utils/DeviceStorage";
import RefundOrderDialog from "../../widget/mine/RefundOrderModal";
import define from "../../utils/define";
import ImagePlaceholder from "../../widget/ImagePlaceholder";
import Tips from "react-native-root-tips";
import LoginViewController from "./login/LoginViewController";
import UseCouponDialog from '../../widget/dialog/UseCouponDialog'
import ShoppingScene from "./shopping/ShoppingScene";
import BuySuccessScene from '../detail/BuySuccessScene'

const APPBAR_HEIGHT = Platform.OS === 'ios' ? 44 : 56;

const {width, height} = Dimensions.get('window')


type Props = {
    navigation:any,
}


export default class MineScene extends PureComponent<Props> {


    static navigationOptions = ({navigation}: any) => ({
        header: null,
    })

    constructor(props) {
        super(props)
        this.state = {
            profile: {},
            isLogin: false,
        }
    }


    componentDidMount() {

        this.getUserInfo()


        DeviceEventEmitter.addListener('loginIn', (a) => {
            console.log('ssscfhugf', a)
            this.setState({isLogin: true, profile: a})
        });
        DeviceEventEmitter.addListener('loginOut', () => {
            this.setState({isLogin: false, profile: {}})
        });

    }


    getUserInfo() {
        // DeviceStorage.get(define.profile)
        //     .then(res=>{
        //         console.log('sss',res)
        //         if(res){
        //             this.setState({isLogin: true,profile:res})
        //         }else {
        //             this.setState({isLogin: false})
        //         }
        //     })
        //     .catch(err=>{
        //         this.setState({isLogin: false})
        //     })
        fetchGetRequest(api.getUserProfile, method.get)
            .then(res => {
                console.log('dcvf', res.profile)
                if (res.profile) {
                    DeviceStorage.save(define.profile, res.profile)
                    this.setState({isLogin: true, profile: res.profile})
                }
            }).catch(err => {
            this.setState({isLogin: false})
        })
    }

    isLogin(login) {
        if (login) {
            return <View style={{alignItems:'center',marginBottom:21,marginTop:32}}>
                <Text style={styles.nick}>{this.state.profile.nickname}</Text>
                <Text style={styles.qian_ming}>{this.state.profile.realname}</Text>
            </View>
        } else {
            return <View
                style={{marginTop:14,flexDirection:'row',justifyContent:'center',alignItems:'center',paddingTop:18,paddingBottom:33}}>
                {/*<TouchableOpacity onPress={()=>{this.props.navigation.navigate('ResigterScene')}}*/}
                {/*style={{width:60,height:25,alignItems:'center',justifyContent:'center',backgroundColor:'rgba(219, 56, 67, 0.2)',borderRadius:3,borderWidth:1,borderColor:'white'}}>*/}
                {/*<Text style={{color:'white',fontSize:13}}>注册</Text>*/}
                {/*</TouchableOpacity>*/}
 <            TouchableOpacity onPress = {
                () => {
                    this.props.navigation.navigate('LoginViewController', {
                        push: true,
                        is_mine:true,
                    })
                }
            }
                                  style={{width:60,height:25,alignItems:'center',justifyContent:'center',backgroundColor:'rgba(219, 56, 67, 0.2)',borderRadius:3,borderWidth:1,borderColor:'white'}}>
                    <Text style={{color:'white',fontSize:13}}>登录</Text>
                </TouchableOpacity>
            </View>
        }

    }

    render() {

        return (
            <ScrollView>
                <View style={styles.container}>
                    <Image style={{width:width,height:220,position:'absolute',}} resizeMode={'stretch'}
                           source={require('../../../res/img/mine/mine_bk.png')}/>
                    <View style={{width:width,alignItems:'center',height:220}}>
                        <Image style={{width:114,height:114,marginTop:31,position:'absolute'}}
                               source={require('../../../res/img/mine/mine_header_bk.png')}/>
                        <ImagePlaceholder styles={{width:82,height:82,marginTop:47,borderRadius:41}}
                                          defaultImagePath={require('../../../res/img/ic_avatar_default.png')}
                                          imageUrl={this.state.profile.portrait}/>
                        {this.isLogin(this.state.isLogin)}
                    </View>
                    <View
                        style={{width:width,height:95,marginTop:0,flexDirection:'row',backgroundColor:"white",justifyContent:'space-around'}}>
                        <TouchableOpacity style={{alignItems:'center',justifyContent:'center',width:60,height:95}}
                                          onPress={()=>{
                        DeviceStorage.get(define.token)
                            .then(res=>{
                            if (res){
                                this.props.navigation.navigate('MineAddressScene',{isOrder:false,callback:(data)=>{
                                        this.getUserInfo()
                                    }})
                            }else {
                                Tips.showWarn('请先登录',{mask:true})
                            }
                        }).catch(err=>{
                        })
                    }}>
                            <Image style={{width:44,height:44}}
                                   source={require('../../../res/img/mine/mine_address.png')}/>
                            <Text style={styles.zhong_title}>我的地址</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={{alignItems:'center',justifyContent:'center',width:60,height:95}}
                                          onPress={()=>{
                                          DeviceStorage.get(define.token).then(res=>{
                                              if (res){
                                                  this.props.navigation.navigate('MineOrderScene',{number:0})
                                              }else {
                                                  Tips.showWarn('请先登录',{mask:true})
                                              }
                                          }).catch(err=>{
                                          })
                                      }}>
                            <Image style={{width:44,height:44}}
                                   source={require('../../../res/img/mine/mine_order.png')}/>
                            <Text style={styles.zhong_title}>我的订单</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={{alignItems:'center',justifyContent:'center',width:60,height:95}}
                                          onPress={()=>{
                                          DeviceStorage.get(define.token).then(res=>{
                                              if (res){
                                                  this.props.navigation.navigate('myHomeworkScene')
                                              }else {
                                                  Tips.showWarn('请先登录',{mask:true})
                                              }
                                          }).catch(err=>{
                                          })
                                      }}>
                            <Image style={{width:44,height:44}}
                                   source={require('../../../res/img/mine/mine_homework.png')}/>
                            <Text style={styles.zhong_title}>我的作业</Text>
                        </TouchableOpacity>
                    </View>
                    <View style={{marginTop:6}}>
                        <MineItemCell image={require('../../../res/img/mine/mine_my.png')} title='个人资料' onPress={()=>{
                        DeviceStorage.get(define.token).then(res=>{
                            console.log('res',res)
                            if (res){
                                fetchGetRequest(api.getUserProfile, method.get)
                                    .then(res => {
                                        if (res.profile) {
                                            this.props.navigation.navigate('personalMateralScene',{profile:res.profile,callback:(data)=>{
                                                    this.getUserInfo()
                                                }})
                                        }
                                    }).catch(err => {
                                    this.setState({isLogin: false})
                                })

                            }else {
                                Tips.showWarn('请先登录',{mask:true})
                            }
                        }).catch(err=>{

                        })
                    }}/>

                        <View style={{backgroundColor:'rgba(0,0,0,0.1)',width:width - 66,height:1,marginLeft:33}}/>

                        <MineItemCell image={require('../../../res/img/mine/miine_coupon.png')} title='优惠券' onPress={()=>{
                        DeviceStorage.get(define.token).then(res=>{
                            if (res){
                                this.props.navigation.navigate('MineCouponScene')
                            }else {
                                Tips.showWarn('请先登录',{mask:true})
                            }
                        }).catch(err=>{
                        })
                    }}/>
                        <View style={{backgroundColor:'rgba(0,0,0,0.1)',width:width - 66,height:1,marginLeft:33}}/>

                        <MineItemCell image={require('../../../res/img/mine/mine_shop.png')} title='购物车' onPress={()=>{
                            this.props.navigation.navigate('ShoppingCart')

                        }}/>

                        <View style={{backgroundColor:'rgba(0,0,0,0.1)',width:width - 66,height:1,marginLeft:33}}/>

                        <MineItemCell image={require('../../../res/img/mine/mine_feeback.png')} title='信息反馈' onPress={()=>{
                        DeviceStorage.get(define.token).then(res=>{
                            if (res){
                                this.props.navigation.navigate('MineInfoFeekback')
                            }else {
                                Tips.showWarn('请先登录',{mask:true})
                            }
                        }).catch(err=>{
                        })
                    }}/>



                        <View style={{backgroundColor:'rgba(0,0,0,0.1)',width:width - 66,height:1,marginLeft:33}}/>

                        <MineItemCell image={require('../../../res/img/mine/mine_guanyu.png')} title='关于我们' onPress={()=>{
                         this.props.navigation.navigate('aboutUsScene')

                        }}/>
                        {this.renderIOS()}
                        <View style={{backgroundColor:'rgba(0,0,0,0.1)',width:width - 66,height:1,marginLeft:33}}/>

                        <MineItemCell image={require('../../../res/img/mine/mine_sitting.png')} title='设置' onPress={()=>{
                            // MineSettingScene
                            this.props.navigation.navigate('MineSettingScene',{callback:(data)=>{
                                    this.setState({isLogin: false,profile:{}})
                                }})
                        }}/>



                    </View>
                </View>
            </ScrollView>
        );
    }
    renderIOS(){
        if (Platform.OS === 'ios') {
            return <View>
                <View style={{backgroundColor:'rgba(0,0,0,0.1)',width:width - 66,height:1,marginLeft:33}}/>

                <MineItemCell image={require('../../../res/img/mine/mine_iap.png')} title='余额' onPress={()=>{
                    DeviceStorage.get(define.token).then(res=>{
                        if (res){
                            this.props.navigation.navigate('IapScene')
                        }else {
                            Tips.showWarn('请先登录',{mask:true})
                        }
                    }).catch(err=>{
                    })
                }}/>
            </View>
        }else {
            return <View/>
        }
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F9F9F9',
    },
    nick: {
        fontWeight: 'bold',
        color: 'white',
        fontSize: 20,
    },
    qian_ming: {
        color: 'rgba(255,255,255,0.8)',
        fontSize: 13,
        marginTop: 6,
    },
    zhong_title: {
        color: 'black',
        fontSize: 13,
        marginTop: 10,
    },
});




